﻿#include <benchmark/benchmark.h>
#include <random>
#include "internal_sort.hpp"

// 生成随机数据的辅助函数
std::vector<int> GenerateRandomArray(size_t size) {
    std::vector<int> arr(size);
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, 10000);
    
    for (size_t i = 0; i < size; i++) {
        arr[i] = dis(gen);
    }
    return arr;
}

// 为每个排序算法创建基准测试
static void BM_InsertSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming(); // 暂停计时器
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming(); // 恢复计时器
        
        insert_sort(arr.data(), arr.size());
    }
}

static void BM_MidInsertSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming();
        
        mid_insert_sort(arr.data(), arr.size());
    }
}

static void BM_ShellSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming();
        
        shell_sort(arr.data(), arr.size());
    }
}

static void BM_SelectSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming();
        
        select_sort(arr.data(), arr.size());
    }
}

static void BM_HeapSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming();
        
        heap_sort(arr.data(), arr.size());
    }
}

static void BM_QuickSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming();
        
        quick_sort(arr.data(), 0, arr.size() - 1);
    }
}

static void BM_MergeSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming();
        
        merge_sort(arr.data(), 0, arr.size() - 1);
    }
}

static void BM_CountSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming();
        
        count_sort(arr.data(), arr.size());
    }
}

static void BM_RadixSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        auto arr = GenerateRandomArray(state.range(0));
        state.ResumeTiming();
        
        radix_sort(arr.data(), arr.size());
    }
}

// 注册基准测试
BENCHMARK(BM_InsertSort)->Range(8, 8<<10);
BENCHMARK(BM_MidInsertSort)->Range(8, 8<<10);
BENCHMARK(BM_ShellSort)->Range(8, 8<<10);
BENCHMARK(BM_SelectSort)->Range(8, 8<<10);
BENCHMARK(BM_HeapSort)->Range(8, 8<<10);
BENCHMARK(BM_QuickSort)->Range(8, 8<<10);
BENCHMARK(BM_MergeSort)->Range(8, 8<<10);
BENCHMARK(BM_CountSort)->Range(8, 8<<10);
BENCHMARK(BM_RadixSort)->Range(8, 8<<10);

BENCHMARK_MAIN();